package top.scsoul.voes.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import top.scsoul.voes.common.core.domain.entity.Permission;
import top.scsoul.voes.common.core.domain.entity.Role;
import top.scsoul.voes.common.utils.ExcelExportDownloadUtil;
import top.scsoul.voes.system.mapper.PermissionMapper;
import top.scsoul.voes.system.service.PermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * (Permission)表服务实现类
 *
 * @author guolei
 * @since 2021-08-8 00:12:59
 */
@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {
    @Resource
    private PermissionMapper permissionMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Permission queryById(Integer id) {
        return this.permissionMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Permission> queryAllByLimit(int offset, int limit) {
        return this.permissionMapper.queryAllByLimit(offset, limit);
    }

    @Override
    public List<Permission> queryAll(Permission permission) {
        return permissionMapper.queryAll(permission);
    }

    @Override
    public PageInfo<Permission> queryAll(String permissionDesc, int currentPage, int pageSize) {
        Permission permission = new Permission();
        permission.setPermissionDesc(permissionDesc);
        PageHelper.startPage(currentPage, pageSize);
        List<Permission> permissions = permissionMapper.queryAll(permission);
        return new PageInfo<Permission>(permissions);
    }

    /**
     * 新增数据
     *
     * @param permission 实例对象
     * @return 实例对象
     */
    @Override
    public Permission insert(Permission permission) {
        this.permissionMapper.insert(permission);
        return permission;
    }

    /**
     * 修改数据
     *
     * @param permission 实例对象
     * @return 实例对象
     */
    @Override
    public Permission update(Permission permission) {
        this.permissionMapper.update(permission);
        return this.queryById(permission.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.permissionMapper.deleteById(id) > 0;
    }

    @Override
    public void exportRole(List<Integer> ids, HttpServletResponse response) throws IOException {
        List<Permission> permissions = permissionMapper.queryByIds(ids);
        ExcelExportDownloadUtil.excelExport(response, permissions, Permission.class,"权限信息表");
    }
}